package array;

/**
 * @description: 27. 移除元素
 * https://leetcode.cn/problems/remove-element/description/
 * @author: caoyihong
 * @date: 2025-10-09 10:36
 * @Version 1.0
 **/
public class RemoveElement {
    public static void main(String[] args) {
        String[] inputs = args[0].substring(1,args[0].length()-1).split(",");
        int[] nums = new int[inputs.length];
        for (int i = 0; i < inputs.length; i++) {
            nums[i] = Integer.parseInt(inputs[i]);
        }
        int target = Integer.parseInt(args[1]);

        int i = 0, j = nums.length-1;
        while (i <= j) {
            if (nums[i] != target && i <= j) {
                i++;
            }
            // 找到了第一个相等值的i

            if (nums[j] == target && i <= j) {
                j--;
            }
            // 找到了第一个不想等值的j
            if (i < j) {
                int tmp = nums[i];
                nums[i] = nums[j];
                nums[j] = tmp;
            }

        }
        System.out.println(i);
    }
}
